package com.plian.system.config;

import com.plian.Tools.TokenUtil;
import com.plian.system.config.exception.MyRuntimeException;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ObjResult;
import com.plian.system.properties.GzwProperties;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class HasGZWRoleAspect {

    @Autowired
    private GzwProperties gzwProperties;

    @Around("@annotation(com.plian.system.config.HasGZWRole) || @within(com.plian.system.config.HasGZWRole)")
    public Object preAuth(ProceedingJoinPoint point) throws Throwable {
        if (this.handleAuth(point)) {
            return point.proceed();
        } else {
            throw new MyRuntimeException(new ObjResult(CommonCode.UNAUTHORISE));
        }
    }

    private boolean handleAuth(ProceedingJoinPoint point) {
        return gzwProperties.getId().equals(TokenUtil.getTokenOrgId());
    }
}
